package top.txl.controller;

import io.jsonwebtoken.Claims;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.txl.entity.Category;
import top.txl.entity.Flavors;
import top.txl.entity.PageBean;
import top.txl.entity.R;
import top.txl.exception.CustomerException;
import top.txl.service.CategoryService;
import top.txl.utils.HandlerUpdateInfo;
import top.txl.utils.JwtUtils;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;


@CrossOrigin
@Slf4j
@RequestMapping("/category")
@RestController
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @PostMapping("/add")
    public R<String> add(HttpServletRequest request, @RequestBody Category category){
        String jwt = request.getHeader("Authorization");
        Claims claims = JwtUtils.parseJwt(jwt);
        Integer id = (Integer)claims.get("id");
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(id);
        category.setUpdateUser(id);
        if(category.getType() == null || category.getType().equals("")){
            category.setSort(0);
        }
        categoryService.add(category);
        return R.success("添加成功");
    }

    @PutMapping("/edit")
    public R<String> edit(HttpServletRequest request ,@RequestBody Category category){
        String jwt = request.getHeader("Authorization");
        Claims claims = JwtUtils.parseJwt(jwt);
        Integer id = (Integer)claims.get("id");

        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(id);
        if(category.getType() == null || category.getType().equals("")){
            category.setSort(0);
        }
        categoryService.edit(category);
        return R.success("修改成功");
    }

    @DeleteMapping("/delete/{id}")
    public R<String> delete(@PathVariable Integer id){
        categoryService.delete(id);
        return R.success("删除成功");
    }

    @GetMapping("/lists")
    public R<PageBean> lists(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "5") Integer pageSize,
                             @RequestParam(defaultValue = "0") Integer type){

        log.info("type参数，{}",type);
        if(!type.equals(0) && !type.equals(1) && !type.equals(2) && !type.equals(3)){
            throw new CustomerException("分类参数-type-只能为0/1/2");
        }

        // 判断，假如请求的是有类型的要返回全部的
        if(type.equals(1) || type.equals(2) || type.equals(3)){
            page = 1;
            pageSize = 100;
        }

        if(type.equals(3)){
            type = 0;
        }

        PageBean pageBean = categoryService.lists(page,pageSize,type);
        return R.success(pageBean);
    }

    @GetMapping("/lists/{id}")
    public R<Category> detail(@PathVariable Integer id){
        Category category = categoryService.detail(id);
        return R.success(category);
    }

    /**
     * 口味
     */
    @GetMapping("/flavors")
    public R<List<Flavors>> flavors(){
        List<Flavors> lists = categoryService.flavors();
        return R.success(lists);
    }
}
